package com.base.server.search.service.impl;

import java.io.File;
import java.util.List;

import org.springframework.stereotype.Component;

import com.base.server.search.pojo.SearchModel;
import com.base.server.search.service.SearchService;
import com.base.server.search.utils.LuceneUtil;
import com.ms.env.EnvUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;

@Component
public class SearchServiceImpl implements SearchService {

	@Override
	public ResponseFrame createIndex(SearchModel searchModel, String system) {
		String indexPath = EnvUtil.searchIndexPath() + File.separator + system + File.separator;
		ResponseFrame frame = new ResponseFrame();
		LuceneUtil.createIndex(searchModel, indexPath);
		frame.setSucc();
		return frame;
	}

	@Override
	public ResponseFrame updateIndex(SearchModel searchModel, String system) {
		String indexPath = EnvUtil.searchIndexPath() + File.separator + system + File.separator;
		ResponseFrame frame = new ResponseFrame();
		LuceneUtil.updateIndex(searchModel, indexPath);
		frame.setSucc();
		return frame;
	}

	@Override
	public ResponseFrame deleteIndex(SearchModel searchModel, String system) {
		String indexPath = EnvUtil.searchIndexPath() + File.separator + system + File.separator;
		ResponseFrame frame = new ResponseFrame();
		LuceneUtil.deleteIndex(searchModel, indexPath);
		frame.setSucc();
		return frame;
	}

	@Override
	public ResponseFrame search(String searchString, String searchField, String system) {
		List<String> searchStrings = FrameStringUtil.toArray(searchString, ",");
		List<String> searchFields = FrameStringUtil.toArray(searchField, ",");
		String indexPath = EnvUtil.searchIndexPath() + File.separator + system + File.separator;
		ResponseFrame frame = new ResponseFrame();
		List<SearchModel> list = LuceneUtil.search(searchStrings.toArray(new String[searchStrings.size()]), searchFields.toArray(new String[searchFields.size()]), indexPath);
		frame.setBody(list);
		frame.setSucc();
		return frame;
	}

}